//
//  MBProgressHUD+QXUtils.m
//  Pods
//
//  Created by Qianxia on 2016/12/23.
//
//

#import "MBProgressHUD+QXUtils.h"
#import "QXHUD.h"

@implementation MBProgressHUD (QXUtils)

/**
 *  文本提示
 *
 *  @param text     提示的文本
 *  @param view     提示所在的view
 *  @param duration 隐藏的时间
 *
 */
+ (instancetype)showWithText:(NSString *)text onView:(UIView *)view hiddenDuration:(CGFloat)duration {
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.detailsLabel.text = text;
    hud.detailsLabel.font = [UIFont boldSystemFontOfSize:16];
    hud.contentColor = [UIColor whiteColor];
    hud.bezelView.color = [UIColor colorWithWhite:0 alpha:0.75];
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    // YES代表需要蒙版效果
    hud.dimBackground = NO;
    
    // 主线程执行
    dispatch_async(dispatch_get_main_queue(), ^{
        [hud hideAnimated:YES afterDelay:duration];
    });
    
    return hud;
}

/**
 文本显示
 
 @param text 文本内容
 @return 显示文本内容1.5秒
 */
+ (instancetype)showWithText:(NSString *)text {
    return [MBProgressHUD showWithText:text onView:nil hiddenDuration:2];
}

/**
 显示加载中
 
 @param text 加载内容
 @param view 要加载的界面
 @return MBProgressHUD instance
 */
+ (instancetype)loadingWithText:(NSString *)text onView:(UIView *)view {
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.detailsLabel.text = text;
    hud.detailsLabel.font = [UIFont boldSystemFontOfSize:16];
    hud.contentColor = [UIColor whiteColor];
    hud.bezelView.color = [UIColor colorWithWhite:0 alpha:0.75];
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    // YES代表需要蒙版效果
    hud.dimBackground = NO;
    hud.userInteractionEnabled = YES;
    [hud setSquare:YES];
    
    return hud;
}

+ (void)hiddenHUDForView:(UIView *)view {
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    
    // 主线程执行
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:view animated:YES];
    });
}


+ (void)hiddenAllHUDForView:(UIView *)view animated:(BOOL)animated {
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    
    // 主线程执行
    dispatch_async(dispatch_get_main_queue(), ^{
        NSArray *subviews = view.subviews;
        for (UIView *aView in subviews) {
            if ([aView isKindOfClass:self]) {
                MBProgressHUD *hud = (MBProgressHUD *)aView;
                hud.removeFromSuperViewOnHide = YES;
                [hud hideAnimated:animated];
            }
        }
    });

}

/**
 提示框
 
 @param mode 提示类型
 @param text 提示内容
 @param view 要加载的界面
 @param duration 隐藏时间
 @return return value description
 */
+ (instancetype)showHUDWithMode:(QXHUDMode)mode text:(NSString *)text onView:(UIView *)view hiddenDuration:(CGFloat)duration {
    
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    hud.detailsLabel.text = text;
    hud.detailsLabel.font = [UIFont boldSystemFontOfSize:14];
    
    if (mode == (QXHUDModeErrorForWhite || QXHUDModeAlertForWhite || QXHUDModeSuccessfulForWhite)) {
        hud.contentColor = [[UIColor blackColor] colorWithAlphaComponent:0.8];
        hud.bezelView.color = [UIColor whiteColor];
    } else {
        hud.contentColor = [UIColor whiteColor];
        hud.bezelView.color = [UIColor colorWithWhite:0 alpha:0.75];
    }
    
    // 图片
    hud.customView = [[UIImageView alloc] initWithImage:[self imageWithMode:mode]];
    
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    // YES代表需要蒙版效果
    hud.dimBackground = NO;
    [hud setSquare:YES];
    
    // 主线程执行
    dispatch_async(dispatch_get_main_queue(), ^{
        [hud hideAnimated:YES afterDelay:duration];
    });
    
    return hud;
}

+ (UIImage *)imageWithMode:(QXHUDMode)mode {
    
    NSBundle *podBundle = [NSBundle bundleForClass:[QXHUD class]];
    NSURL *bundleURL = [podBundle URLForResource:@"MBProgressHUD_QXUtils" withExtension:@"bundle"];
    NSBundle *resourceBundle = [NSBundle bundleWithURL:bundleURL];
    
    switch (mode) {
        case QXHUDModeSuccessfulForBlack:
            return [UIImage imageNamed:@"chenggong_white" inBundle:resourceBundle compatibleWithTraitCollection:nil];
        case QXHUDModeSuccessfulForWhite:
            return [UIImage imageNamed:@"chenggong" inBundle:resourceBundle compatibleWithTraitCollection:nil];
        case QXHUDModeErrorForBlack:
            return [UIImage imageNamed:@"tishi_white" inBundle:resourceBundle compatibleWithTraitCollection:nil];
        case QXHUDModeErrorForWhite:
            return [UIImage imageNamed:@"tishi" inBundle:resourceBundle compatibleWithTraitCollection:nil];
        case QXHUDModeAlertForBlack:
            return [UIImage imageNamed:@"jinggao_white" inBundle:resourceBundle compatibleWithTraitCollection:nil];
        case QXHUDModeAlertForWhite:
            return [UIImage imageNamed:@"jinggao" inBundle:resourceBundle compatibleWithTraitCollection:nil];
    }
}

@end
